package com.wangkang.eduservice.controller;
import com.wangkang.commonutils.R;
import com.wangkang.eduservice.entity.subject.OneSubject;
import com.wangkang.eduservice.enums.EduResultCode;
import com.wangkang.eduservice.service.EduSubjectService;
import com.wangkang.servicebase.exceptionhandler.exception.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 课程科目 前端控制器
 * </p>
 *
 * @author wangkang
 * @since 2022-04-11
 */
@RestController
@RequestMapping("/eduservice/subject")
//@CrossOrigin
@Api(tags = "课程分类管理")
public class EduSubjectController {

    @Autowired
    private  EduSubjectService eduSubjectService;

    public EduSubjectController(EduSubjectService subjectService) {
        this.eduSubjectService = subjectService;
    }


    @PostMapping("/addSubject")
    @ApiOperation("上传Excel添加科目")
    public R addSubject(MultipartFile file){

        file = Optional.ofNullable(file).orElseThrow(() -> GuliException.from(EduResultCode.FILE_IS_EMPTY));
        //上传过来的excel文件
        eduSubjectService.saveSubject(file,eduSubjectService);
        return R.ok();
    }


    @ApiOperation("获取所有分类")
    @GetMapping("/findAllSubject")
    public R findAllSubject(){
        //集合的泛型写一级分类，因为每个一级分类中有很多二级分类
        List<OneSubject> allOneAndTwoSubject = eduSubjectService.getAllOneAndTwoSubject();

        return R.ok().data("items",allOneAndTwoSubject);
    }

}